package com.dzf.controller;


import com.dzf.entity.Book;
import com.dzf.entity.LargeFile;
import com.dzf.entity.RespJson;
import com.dzf.service.BookService;
import com.dzf.service.LargeFileService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@CrossOrigin //允许跨域
@RestController
@RequestMapping("/api/adm/book")
public class AdmBookController {
    @Autowired
    BookService bookSerice;
    @Autowired
    LargeFileService largeFileService;
    @Autowired
    ObjectMapper objectMapper;

    @RequestMapping("/findAll")
    public RespJson findAll()
    {
        return RespJson.pack(200,"OK",bookSerice.findAll());
    }

    @RequestMapping("/findById/{bookid}")
    public RespJson findById(@PathVariable("bookid") int bookid)
    {
        return RespJson.pack(200,"OK",bookSerice.findById(bookid));
    }

    @RequestMapping("/findByPage/{start}/{num}")
    public RespJson findByPage(@PathVariable("start") int start,
                             @PathVariable("num")int num)
    {

        Map<String, Object> bookmap = bookSerice.findBookByPage(start, num, 0);



        return RespJson.pack(200,"",bookmap);
    }


    @PostMapping("/addBook")
    public RespJson amend(Book book, @RequestParam(value = "pic",required = false) MultipartFile pic)
    {
        System.err.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+book.getId());
        UUID uuid = UUID.randomUUID();
        LargeFile largeFile = new LargeFile();
        try {
            if (pic!=null&&pic.getSize()>0)
            {
                largeFile.setId(uuid.toString());
                largeFile.setFilename(pic.getOriginalFilename());
                largeFile.setContent(pic.getBytes());
                largeFileService.addLargeFile(largeFile);//文件内容
            }

            if (largeFile.getId()!=null ||!" ".equals(largeFile.getId()))
            {
                book.setPicture(uuid.toString());
            }
            bookSerice.amend(book);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return RespJson.pack(200,"添加成功",bookSerice.findById(book.getId()));
    }

    @RequestMapping("show/{id}")
    public void show(@PathVariable("id")String id,
                     HttpServletResponse response)
    {

            LargeFile largeFile = largeFileService.findByPic(id);
//        System.out.println(largeFile);
//        System.out.println(book.getPicture());
            if (largeFile==null)return;//对象为空则结束方法
            try {
                response.setContentType("image/jpeg");//将图片以jpg形式返回
                response.getOutputStream().write((byte[]) largeFile.getContent());//获取response的输出流，写出图片的内容
            } catch (Exception e) {
                e.printStackTrace();
            }
    }



    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
